package mobi.bantu.bean;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by tancw on 2017/2/10.
 */
public abstract class BaseController {

    public String getIp(HttpServletRequest request) {
        if (request.getHeader("x-forwarded-for") == null) {
            return request.getRemoteAddr();
        }
        return request.getHeader("x-forwarded-for");
    }

    public String getLocalIp(HttpServletRequest request) {
        return request.getLocalAddr();
    }

    public User getSessionUser(HttpServletRequest request) {
        return (User) request.getSession().getAttribute(Cons.SESSION_USER);
    }

    protected void setSessionUser(HttpServletRequest request, User user) {
        request.getSession().setMaxInactiveInterval(120 * 60); // 当前会话的失效时间两个小时
        request.getSession().setAttribute(Cons.SESSION_USER, user);
    }

    protected boolean unRegister(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(Cons.SESSION_USER);
        return user == null || user.getId() == null;
    }
}
